//
//  AdGADInterstitial.m
//  PaintWar
//
//  Created by Tai Le on 10/10/14.
//
//

#import "AdGADInterstitial.h"

@implementation AdGADInterstitial

- (id)init
{
    self = [super init];
    if (self)
    {
        [self cycleAds];
    }
    
    return self;
}

- (BOOL)showAdInterstitialInViewController:(UIViewController *)controller
{
    if (controller == nil)
    {
        return NO;
    }
    
    if (_interstitial.isReady)
    {
        [_interstitial presentFromRootViewController:controller];
        
        return YES;
    }
    
    return NO;
}

- (BOOL)showAdInterstitialInView:(UIView *)view
{
    NSLog(@"[%@] does not support", self.class);
    
    return NO;
}

- (void)cycleAds
{
    [super cycleAds];
    
    _interstitial = [[GADInterstitial alloc] init];
    _interstitial.adUnitID = INTERSTITIAL_AD_UNIT_ID;
    _interstitial.delegate = self;
    
    GADRequest *request = [GADRequest request];
    
#ifndef __NDEBUG__
    request.testDevices = @[@"68a5ed350ba502938920ea2ea9b845b8", @"da26682b6f3001cee24b541b7d484d20" ];
#endif
    
    [_interstitial loadRequest:request];
}

- (id)getAds
{
    return _interstitial;
}

- (void)releaseAds
{
    [super releaseAds];
    
    _interstitial.delegate = nil;
    _interstitial = nil;
}

#pragma mark - Delegate
- (void)interstitialDidReceiveAd:(GADInterstitial *)ad
{
    [self raiseDidLoad:ad];
}

- (void)interstitial:(GADInterstitial *)ad didFailToReceiveAdWithError:(GADRequestError *)error
{
   [self raiseDidFail:ad error:error];
}

- (void)interstitialWillDismissScreen:(GADInterstitial *)interstitial
{
    [self raiseDidUnload:interstitial];
}

@end
